<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="初试情况" v-if="step ==1 || step==4">
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item
                  label="面试时间"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="firstInterviewTime"
                >
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择面试时间"
                    v-model="model.firstInterviewTime"
                    :show-time="true"
                    date-format="YYYY-MM-DD HH:mm"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="面试官" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="firstInterviewer">
                  <select-user-by-dep :disabled=formDisabled  :multi="false"
                  v-model="model.firstInterviewer" ></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="交接人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="firstExchanger">
                  <select-user-by-dep :disabled=formDisabled  :multi="false"
                  v-model="model.firstExchanger" ></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="初试结果"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="firstResult"
                >
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.firstResult"
                    dictCode="interview_result"
                    placeholder="请选择初试结果"
                  />
                </a-form-model-item>
              </a-col>
              <!-- <a-col :span="8">
                <a-form-model-item label="所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
                  <select-team-input
                    :disabled=formDisabled
                    v-model="model.teamId" :tdlx="3"/>
                </a-form-model-item>
              </a-col> -->
              <a-col :span="24">
                <a-form-model-item label="初试记录" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="firstRemark">
                  <a-textarea
                    :disabled="formDisabled"
                    v-model="model.firstRemark"
                    rows="6"
                    placeholder="请输入初试记录"
                    :max-length="500"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="复试情况"  v-if="step==2 || step==4">
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item
                  label="复试时间"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="secondInterviewTime"
                >
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择复试时间"
                    v-model="model.secondInterviewTime"
                    :show-time="true"
                    date-format="YYYY-MM-DD HH:mm"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="面试官"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="secondInterviewer"
                >
                <select-user-by-dep :disabled=formDisabled  :multi="false"
                  v-model="model.secondInterviewer" ></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="交接人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="secondExchanger">
                  <select-user-by-dep :disabled=formDisabled  :multi="false"
                  v-model="model.secondExchanger" ></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="复试结果"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="secondResult"
                >
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.secondResult"
                    dictCode="interview_result"
                    placeholder="请选择复试结果"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="复试记录" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="secondRemark">
                  <a-textarea
                    :disabled="formDisabled"
                    v-model="model.secondRemark"
                    rows="6"
                    placeholder="请输入复试记录"
                    :max-length="500"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="3" header="HR评价"  v-if="step==3 || step==4">
            <a-row :gutter="48">
              <!-- <a-col :span="8" >
                <a-form-model-item label="是否参加面试" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isJoin">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.isJoin"
                    dictCode="YES_OR_NO"
                    placeholder="请选择是否参加面试"
                  />
                </a-form-model-item>
              </a-col> -->

              <a-col :span="8">
                <a-form-model-item
                  label="面试结果"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="thirdResult"
                >
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.thirdResult"
                    dictCode="interview_result"
                    placeholder="请选择面试结果"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="到岗时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="arrivalTime">
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择到岗时间"
                    v-model="model.arrivalTime"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="实际面试时间"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="actualInterviewTime"
                >
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择实际面试时间"
                    v-model="model.actualInterviewTime"
                    :show-time="true"
                    date-format="YYYY-MM-DD HH:mm"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="期望薪资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salary">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    v-model="model.salary"
                    placeholder="请输入期望薪资"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="人才标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="talentLabel">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.talentLabel"
                    dictCode="talent_label"
                    placeholder="请选择人才标签"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="综合评价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                  <a-textarea :disabled="formDisabled" v-model="model.remark" rows="6" placeholder="请输入综合评价" :max-length="500"/>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
        <a-input :disabled="formDisabled" type="hidden" v-model="model.applyId"  />
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import SelectPersonInput from '@/views/scy/components/SelectPersonInput'
import SelectTeamInput from '@/views/scy/components/SelectTeamInput';

export default {
  name: 'ErpHrApplyResultInfoForm',
  components: {
    SelectPersonInput,
    SelectUserByDep,
    SelectTeamInput
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      step:1,
      activeKey: ['1', '2', '3'],
      model: {
        'isJoin':"1"
      },
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        firstInterviewTime: [
            { required: true, message: '请选择面试时间!'},
        ],
        firstInterviewer: [
            { required: true, message: '请选择面试官!'},
        ],
        firstRemark: [
            { required: true, message: '请输入初试记录!'},
        ],
        teamId: [
          { required: true, message: '请选择所属团队!'},
        ],
        secondInterviewTime: [
            { required: true, message: '请选择复试时间!'},
        ],
        secondInterviewer: [
            { required: true, message: '请选择面试官!'},
        ],
        secondRemark: [
            { required: true, message: '请输入复试记录!'},
        ],
        isJoin: [
            { required: true, message: '请选择是否参加面试!'},
        ],
        firstResult: [
            { required: true, message: '请选择初试结果!'},
        ],
        secondResult: [
            { required: true, message: '请选择复试结果!'},
        ],
        thirdResult: [
            { required: true, message: '请选择评价结果!'},
        ],
        interviewResult: [
            { required: true, message: '请选择面试结果!'},
        ],
        arrivalTime: [
            { required: true, message: '请输入预计到岗时间!'},
        ],
        actualInterviewTime: [
            { required: true, message: '请输入实际面试时间!'},
        ],
        salary: [
            { required: true,pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
        ],
        remark: [
            { required: true, message: '请输入综合评价!'},
        ]
      },
      url: {
        add: '/api/erp/hr/apply/erpHrApplyResultInfo/add',
        edit: '/api/erp/hr/apply/erpHrApplyResultInfo/edit',
        queryById: '/api/erp/hr/apply/erpHrApplyResultInfo/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  watch: {
    activeKey(key) {
      this.$forceUpdate()
    },
  },
  methods: {
    add(applyId,step) {
      this.edit(this.modelDefault)
      this.step=step;
      this.model.applyId=applyId;
    },
    edit(record,step) {
      console.log(step);
      if(step){
        this.step=step;
      }
      this.$refs.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          //初试未通过
          if(this.model.firstResult=='2'){
            this.model.interviewResult="2"
          }
          //复试未通过
          if(this.model.secondResult=='2'){
            this.model.interviewResult="3"
          }
          //评价未通过
          if(this.model.thirdResult=='2'){
            this.model.interviewResult="4"
          }else if(this.model.thirdResult=='1'){
            this.model.interviewResult="1"
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
<style>
 * {
        -webkit-print-color-adjust: exact !important;   /* Chrome, Safari */
        color-adjust: exact !important;                 /*Firefox*/
   }

</style>